// pages/price/calendar/calendar.js
// const axios = require('../../../utils/axios.js')
Page({
 
  
  /**
   * 组件的初始数据
   */
  data: {
    calenderOptins:{
      len:"CN",//CN EN
      maxMonth: 10,//number
    },
    // roomPricePlanId:1,
    weekArray:{
      CN: [
        {code:0,text:"日"},
        {code: 1,text: "一"},
        {code: 2,text: "二"},
        {code: 3,text: "三"},
        {code: 4,text: "四"},
        {code: 5,text: "五"},
        {code: 6, text: "六"}
      ],
      EN: [
        { code: 0, text: "Sun" },
        { code: 1, text: "Mon" },
        { code: 2, text: "Tue" },
        { code: 3, text: "Wed" },
        { code: 4, text: "Thu" },
        { code: 5, text: "Fri" },
        { code: 6, text: "Sat" }
      ],
    },
    
    calenderData:[],
    weekData:[],//周数据
    nowDate:{},//当天数据
    isFirstClick:true,
    arriveDate: { timeNum: 0 },//到达日期
    leaveDate: { timeNum: 0 },//离店日期
  },
  onReady:function(){
    wx.setNavigationBarTitle({
      title: '选择日期'
    })
  },
  onLoad: function (options) {
  
    let nowDate = this.getNowDate();
    // var roomPricePlanId = options.id;
    this.setData({
     
      nowDate: nowDate,
      
      // roomPricePlanId: roomPricePlanId
    })
    let weekData = this.getWeekArray();
    let dateArry = this.getDateArry();
    this.setData({
      weekData: weekData,

      calenderData: dateArry,

    })
    // this.getPrice()
   
    
  },
  
    //itemClass
    itemClass(){
      return "dd"
    },
    //获取周列表
    getWeekArray(){     
      let len = this.data.calenderOptins.len;
      return this.data.weekArray[len];
    },
    //获取日期数据
    getDateArry(){
      let nowDate = this.getNowDate();
      let monthData = this.getNowMonth();
      let dataArray = [];
      monthData.forEach(element => {
        let days = this.getDaysArrayByMon(element.year, element.month);
        let obj={
          titleText: element.year + "-" + (element.month < 10 ? "0" + element.month : element.month),
          year: element.year,
          month: element.month,
          days,
        }
        dataArray.push(obj);
      })
     
      
      return dataArray
    },
  getMonthArry() {
    let nowDate = this.getNowDate();
    let monthData = this.getNowMonth();
    let dataArray = [];
    monthData.forEach(element => {
     
      let obj = {
        titleText: element.year + "-" + (element.month < 10 ? "0" + element.month : element.month),
        year: element.year,
        month: element.month,
        
      }
      dataArray.push(obj);
    })

    console.log(dataArray)
    return dataArray
  },
    //根据年月获取本月日期
    getDaysArrayByMon(year,month){
      month = parseInt(month, 10);
      let myDate = new Date(year, month, 0)
      let week = myDate.getDay();
      let num = myDate.getDate();
      let data = [];
      
      for (let k = 1; k < num+1;k++){
        let time = year + "-" + (month < 10 ? "0" + month : month) + "-" + (k < 10 ? "0" + k : k);
        let timeNum = new Date(time).getTime()
        let week = this.getNowDate(time).week;
        let obj = {
          year,
          month,
          week,
          price: "",
          day: k,
          time,
          timeNum,
        }
        data.push(obj)
        
      }
   
      data = this.unShiftDays(data);
      data = this.pushDays(data);

      return data
    },
  findPrice(time) {
    // console.log(this.data)
    let price = "";
    this.data.priceData.forEach(element => {
      if (element.date === time) {
        price = element.price
      }
    });
    return price

  },
    //头部追加
    unShiftDays(dateArray){
      let newArray = dateArray;
      let firstDay = dateArray[0];
      let week = firstDay.week;
      for (let j = 0; j < week;j++){
        newArray.unshift({
          year:"",
          month: "",
          day: "",
          week: "",
          date: "",
          price:"",
          timeNum:-1,
          isNull: true
        });
      }
      // console.log(firstDay)
      return newArray
     
    },

    //尾部追加
    pushDays(dateArray) {
      let newArray = dateArray;
      let lastDay = dateArray[dateArray.length-1];
      let week = lastDay.week;
      for (let j = 6; j > week; j--) {
        newArray.push({
          year: "",
          month: "",
          day: "",
          week: "",
          date: "",
          price: "",
          timeNum: -1,
          isNull: true
        });
      }
     
      return newArray
      
    },
    //获取月列表
    getNowMonth(){
      let nowDate = this.getNowDate();
      let dataArray = [];
      let month = nowDate.month-1;
      let year = nowDate.year;
      for (let k = 0; k < this.data.calenderOptins.maxMonth; k++) {
        
        year = month+1 > 12 ? year + 1 : year;
        month = month + 1 > 12 ? 1 : month + 1;
        let obj = {
          time: year + "-" + (month < 10 ? "0" + month : month),
          year,
          month
        }
        dataArray.push(obj);      
      }
      return dataArray;
    },
    //获取当天
    getNowDate(newDate){
      let myDate = new Date();
      if (newDate){
        myDate = new Date(newDate);
      }
      let year = myDate.getFullYear();    //获取完整的年份(4位,1970-????)
      let month = myDate.getMonth() + 1;       //获取当前月份(0-11,0代表1月)
      let day = myDate.getDate();        //获取当前日(1-31)
      let week = myDate.getDay();         //获取当前星期X(0-6,0代表星期天)
      let date = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
      let timeNum = new Date(date).getTime();
      return{
        year,
        month,
        day,
        week,
        date,
        price: "222.0",
        isNull:false,
        timeNum,
      }
    },
    itemClick(e){
      let item = e.currentTarget.dataset.itemchild
      let isfirst = this.data.isFirstClick;
      if (item.timeNum < this.data.nowDate.timeNum){
        return
      }
      if (isfirst){
        this.setData({
          arriveDate: item,
          leaveDate: { timeNum: 0 },
          isFirstClick: false
        })
        return
      }
      if (!isfirst){
        if (item.timeNum < this.data.arriveDate.timeNum){
          this.setData({
            arriveDate: item,
            leaveDate: { timeNum:0},
            isFirstClick: false
          })
        }else{
          this.setData({
            leaveDate: item,
            isFirstClick: true
          })
        }
        return
        
      }
    },
    //给上一个页面传递参数
 jumpback: function () {
   let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。

   let prevPage = pages[pages.length - 2];
   let jitian = this.data.leaveDate.day-this.data.arriveDate.day;
   console.log(this.data.leaveDate)
  //  debugger
   prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
       arriveDate: this.data.arriveDate.time,
       leaveDate: this.data.leaveDate.time,
       jitian:jitian,
       week: this.data.leaveDate.week,
      startmonth: this.data.arriveDate.month,
      startday: this.data.arriveDate.day,
      endmonth: this.data.leaveDate.month,
      endday: this.data.leaveDate.day
   })
   wx.navigateBack({
     delta: 1, // 回退前 delta(默认为1) 页面
   });
    },

  


  

})
